/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import dbhandler.DBConnector;
import java.io.*;
import java.net.*;
import java.security.SecureRandom;
import java.util.regex.*;
import org.apache.commons.lang.WordUtils;
import java.util.*;
import java.io.IOException;
import java.util.Arrays;
import dbhandler.DBConnector;
import java.security.NoSuchAlgorithmException;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.lang.String;

/**
 *
 * @author Tokino
 */
public class EditProduct {

    private String title;
    private String mtype;
    private String password;
    private int accounttype;
    private String category;
    private Hashtable errors;
    private String synopsis;
    private String addinfo;
    private String price;

    public EditProduct() {
        title = "";
        mtype = "";
        password = "";
        category = "";
        price = "";
        addinfo = "";
        synopsis = "";
        accounttype = 0;

        errors = new Hashtable();
    }

    public boolean eproduct(String userName) throws SQLException, ClassNotFoundException, IOException, NoSuchAlgorithmException {

        boolean bool = true;
        String digest = null;
        String salt = null;


        DBConnector dbConnectorchk = null;
        dbConnectorchk = new DBConnector("localhost", 3306, "selector", "53l3ct0r", "bookaholic");

        dbConnectorchk.connectToDB();
        CallableStatement callable = null;
        ResultSet result = null;
        String authCall = "{ call login2(?) }";
        callable = dbConnectorchk.getConnection().prepareCall(authCall);
        callable.setString(1, userName);
        result = callable.executeQuery();
        if (result.next()) {
            digest = result.getString("hashpassword");
            salt =
                    result.getString("salt");

        }
        byte[] bDigest =
                dbConnectorchk.base64ToByte(digest);
        byte[] bSalt =
                dbConnectorchk.base64ToByte(salt);
        byte[] proposedDigest =
                dbConnectorchk.getHash(password, bSalt);



        if (accounttype == 3) {
            category = "Book";
            if (!(mtype.equals("Paperback") || mtype.equals("Mass-market") || mtype.equals("Hardcover"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }
        //Fashion Home & Garden Entertainment Cooking Craft
        if (accounttype == 4) {
            category = "Magazine";
            if (!(mtype.equals("Fashion") || mtype.equals("Home & Garden") || mtype.equals("Entertainment") || mtype.equals("Cooking") || mtype.equals("Craft"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }
        if (accounttype == 5) {
            category = "Audio CD";
            if (!(mtype.equals("CD") || mtype.equals("MP3"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }
        if (accounttype == 6) {
            category = "Movie";
            if (!(mtype.equals("DVD") || mtype.equals("Blue-ray"))) {
                mtype = "0";
                errors.put("mtype", "Enter a valid type");
            }
        }

        title = WordUtils.capitalizeFully(title);
        if (title.equals("") || !(title.matches("(?=^.{1,}$)(?=[a-zA-Z0-9]+(([\\,\\.\\-\\s\\!][a-zA-Z0-9])?[a-zA-Z0-9]*)*)(?!.*[\\;\\:\\\"\\<\\>\\(\\)\\{\\}\\[\\]\\@\\#\\$\\%\\^\\`\\~\\=\\|\\/\\\\\\x00-\\x1F]).*$"))) {
            errors.put("title", "Please enter a valid title (alphanumeric (,)(.)(-)( ) only)");
            errors.put("status", "Failure");
            title = "";
            bool = false;
        } else if (!dbConnectorchk.checktitle(title, mtype)) {
            title = "";
            mtype = "0";
            errors = new Hashtable();
            errors.put("title", "title with type does not exists");
            errors.put("mtype", "Enter a valid type");
            errors.put("status", "Failure");
            bool = false;
        }

        if (password.equals("") || password.length() < 7 || !(password.matches("(?=^.{8,}$)(?=.*[A-Z])(?=.*[a-z])(?=.*[\\W_\\d]).*$"))) {
            errors.put("password", "Please enter a valid password (minimum length of 8 characters long containing upper, lower, and 1 numerical/special characters)");
            password = "";
            errors.put("status", "Failure");
            bool = false;
        } else if (!Arrays.equals(proposedDigest, bDigest)) {
            errors.put("password", "password did not match");
            password = "";
            errors.put("status", "Failure");
            bool = false;
        }
        if (bool == true) {
            
            
                    dbConnectorchk.connectToDB();
 
        String authCall2 = "{ call getproductinfo(?,?,?) }";
        callable = dbConnectorchk.getConnection().prepareCall(authCall2);
        callable.setString(1, title);
         callable.setString(2, category);
          callable.setString(3, mtype);
        result = callable.executeQuery();
        while (result.next()) {
synopsis=result.getString("synopsis");
price=result.getString("price");
addinfo=result.getString("addinfo");
        }
            //dbConnectorchk.deletetitle(title,category, mtype);
            //System.out.println("Yesssssssssss");
            //System.out.println(userName);
        }



        return bool;
    }

    public String getErrorMsg(String s) {
        String errorMsg = (String) errors.get(s.trim());
        return (errorMsg == null) ? "" : errorMsg;
    }

    public void setErrors(String key, String msg) {
        errors.put(key, msg);
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getMtype() {
        return mtype;
    }

    public void setMtype(String mtype) {
        this.mtype = mtype;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getAccounttype() {
        return accounttype;
    }

    public void setAccounttype(int accounttype) {
        this.accounttype = accounttype;
    }

    public String getAddinfo() {
        return addinfo;
    }

    public void setAddinfo(String addinfo) {
        this.addinfo = addinfo;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }
}
